package com.buaa.contentProvider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

import androidx.annotation.Nullable;

/**
 * Created by Administrator on 2016/4/23.
 */
public class StudentProvider extends ContentProvider {

    private final String  TAG="StudentProvider";
    private DBHelper dbHelper;

    private static final UriMatcher URI_MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
    private static final int STUDENT=1;//操作单条记录
    private static final int STUDENTS=2;//操作多条记录

    static{
        URI_MATCHER.addURI("com.buaa.contentprovider.StudentProvider","student",STUDENTS);
        URI_MATCHER.addURI("com.buaa.contentprovider.StudentProvider","student/#",STUDENT);
    }

    public StudentProvider(){}

    @Override
    public boolean onCreate() {
        dbHelper=new DBHelper(getContext());
        return true;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        Cursor cursor=null;
        try {
            SQLiteDatabase database=dbHelper.getReadableDatabase();
            int flag=URI_MATCHER.match(uri);
            switch (flag){
                case STUDENT:
                    long id=ContentUris.parseId(uri);
                    String where_value="id="+id;
                    if (selection!=null&&!selection.equals("")){
                        where_value+="and"+selection;
                    }
                    cursor=database.query("student",null,where_value,selectionArgs,null,null,null);
                    break;
                case STUDENTS:
                    cursor=database.query("student",null,selection,selectionArgs,null,null,null);
                    break;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        return cursor;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        int flag=URI_MATCHER.match(uri);
        switch (flag){
            case STUDENT:
                return "vnd.android.cursor.item/student";
            case STUDENTS:
                return "vnd.android.cursor.dir/students";
        }
        return null;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        Uri resultUri=null;
        int flag=URI_MATCHER.match(uri);
        switch (flag){
            case STUDENTS:
                SQLiteDatabase database=dbHelper.getWritableDatabase();
                long id=database.insert("student",null,values);//获得插入记录的当前行号
                resultUri= ContentUris.withAppendedId(uri,id);
                break;
        }
        return resultUri;
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int count=-1;
        try {
            int flag=URI_MATCHER.match(uri);
            SQLiteDatabase database=dbHelper.getWritableDatabase();
            switch (flag){
                case STUDENT:
                    long id=ContentUris.parseId(uri);
                    String where_value="id="+id;
                    if (selection!=null&&!selection.equals("")){
                        where_value+="and"+selection;
                    }
                    count=database.delete("student",where_value,selectionArgs);
                    break;
                case STUDENTS:
                    count=database.delete("student",selection,selectionArgs);
                    break;
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int count=-1;
        try {
            SQLiteDatabase database=dbHelper.getWritableDatabase();
            long id=ContentUris.parseId(uri);
            int flag=URI_MATCHER.match(uri);
            switch (flag){
                case STUDENT:
                    String where_value="id="+id;
                    if (selection!=null&&!selection.equals("")){
                        where_value+="and"+selection;
                    }
                    count=database.update("student",values,where_value,selectionArgs);
                    break;
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return count;
    }
}
